﻿using System;
using System.Linq;

using Fuse8.DomainFramework.Common.Locator;
using Fuse8.DomainFramework.DataAccess;
using Fuse8.DomainFramework.DataAccess.SqlDatabase;

using Fuse8.ContentManagementFramework.Domain.Commands;
using Fuse8.ContentManagementFramework.Domain;

namespace Fuse8.ContentManagementFramework.DataAccess.SqlAccess.Commands
{
    internal class CmsPageMoveDataCommand : CmsPageMoveCommand
    {
        public override IQueryable<CmsPage> Execute()
        {
            var factory = DependencyServiceLocator.Current.GetInstance<IUnitOfWorkFactory<CmsPage>>();

            var currentUnitOfWork = factory.Get(false) as SqlUnitOfWork<CmsPage>;

            if (currentUnitOfWork != null)
            {
                var mapper = DependencyServiceLocator.Current.GetInstance<ISqlEntityDataMapper<CmsPage, int>>();

                SqlUtility<CmsPage, int>.ExecuteNonQuery(
                        currentUnitOfWork.Connection,
                        currentUnitOfWork.Transaction,
                        "cms_sp_PageMove",
                        new
                        {
                            PageId = CommandParameter.PageId,
                            SiblingPageId = CommandParameter.MoveToPageId
                        },
                        mapper);
            }

            return null;
        }
    }
}
